﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zhaoxi.Automatic.Base;
using Zhaoxi.Automatic.Controls;
using Zhaoxi.Automatic.Models;

namespace Zhaoxi.Automatic.ViewModels
{
    internal class MainViewModel : NotifyBase
    {
        //private object _pageContent;

        //public object PageContent
        //{
        //    get { return _pageContent; }
        //    set { _pageContent = value; }
        //}

        private object _pageContent;
        public object PageContent
        {
            get { return _pageContent; }
            set { SetProperty(ref _pageContent, value); }
        }

        private string _total;
        public string Total
        {
            get { return _total; }
            set { SetProperty(ref _total, value); }
        }

        private string _productionCount;
        public string ProductionCount
        {
            get { return _productionCount; }
            set { SetProperty(ref _productionCount, value); }
        }

        private string _badCount;
        public string BadCount
        {
            get { return _badCount; }
            set { SetProperty(ref _badCount, value); }
        }

        public List<MonitorItemModel> Evironment { get; set; }
        public List<MonitorItemModel> DeviceMonitor { get; set; }

        public List<AlarmItemModel> AlarmList { get; set; }

        public ObservableCollection<RaderSeriesModel> RaderDatas { get; set; }

        public List<PersonnelItemModel> PersonnelList { get; set; }

        public List<WorkshopItemModel> WorkshopList { get; set; }

        public List<MachineItemModel> MachineList { get; set; }

        public MainViewModel()
        {
            Total = 197.ToString("0000");

            ProductionCount = 17147.ToString("00000");

            BadCount = 17.ToString("000");


            #region 初始化环境监控
            Evironment = new List<MonitorItemModel>();
            Evironment.Add(new MonitorItemModel { Header = "光照(Lux)" });
            Evironment.Add(new MonitorItemModel { Header = "噪音(db)" });
            Evironment.Add(new MonitorItemModel { Header = "温度(℃)" });
            Evironment.Add(new MonitorItemModel { Header = "湿度(%)" });
            Evironment.Add(new MonitorItemModel { Header = "PM2.5(m³)" });
            Evironment.Add(new MonitorItemModel { Header = "流化氢(ppm)" });
            Evironment.Add(new MonitorItemModel { Header = "氮气(ppm)" });
            #endregion

            Evironment[0].Value = 123;


            #region 初始化消息列表
            AlarmList = new List<AlarmItemModel>();
            AlarmList.Add(new AlarmItemModel { Num = "01", Message = "设备温度过高", Time = "2022-07-05 20:00:00", Duration = 5 });
            AlarmList.Add(new AlarmItemModel { Num = "02", Message = "车间温度过高", Time = "2022-07-05 20:00:00", Duration = 5 });
            AlarmList.Add(new AlarmItemModel { Num = "03", Message = "设备转速过高", Time = "2022-07-05 20:00:00", Duration = 5 });
            #endregion

            #region 初始化设备监控
            DeviceMonitor = new List<MonitorItemModel>();
            DeviceMonitor.Add(new MonitorItemModel { Header = "电能(Kw.h)", Value = 311.2 });
            DeviceMonitor.Add(new MonitorItemModel { Header = "电压(V)", Value = 311.2 });
            DeviceMonitor.Add(new MonitorItemModel { Header = "电流(A)", Value = 311.2 });
            DeviceMonitor.Add(new MonitorItemModel { Header = "压差(kpa)", Value = 311.2 });
            DeviceMonitor.Add(new MonitorItemModel { Header = "温度(℃)", Value = 311.2 });
            DeviceMonitor.Add(new MonitorItemModel { Header = "振动(mm/s)", Value = 311.2 });
            DeviceMonitor.Add(new MonitorItemModel { Header = "转速(r/min)", Value = 311.2 });
            DeviceMonitor.Add(new MonitorItemModel { Header = "气压(kpa)", Value = 311.2 });
            #endregion

            #region 初始化雷达数据 
            RaderDatas = new ObservableCollection<RaderSeriesModel>();
            Random random = new Random();
            for (int i = 1; i <= 7; i++)
            {
                RaderDatas.Add(new RaderSeriesModel { Header = "Tag" + i, Value = random.Next(20, 100) });
            }
            #endregion

            #region 初始化人员缺岗信息
            PersonnelList = new List<PersonnelItemModel>();
            PersonnelList.Add(new PersonnelItemModel { Name = "张三", Duties = "技术员", ManHour = 123 });
            PersonnelList.Add(new PersonnelItemModel { Name = "张三", Duties = "操作员", ManHour = 123 });
            PersonnelList.Add(new PersonnelItemModel { Name = "张三", Duties = "技术员", ManHour = 123 });
            PersonnelList.Add(new PersonnelItemModel { Name = "张三", Duties = "技术员", ManHour = 123 });
            PersonnelList.Add(new PersonnelItemModel { Name = "张三", Duties = "技术员", ManHour = 123 });
            #endregion

            #region 初始化车间列表 
            WorkshopList = new List<WorkshopItemModel>();
            WorkshopList.Add(new WorkshopItemModel { Name = "贴片车间", TotalCount = 45, WorkCount = 32, WaitCount = 8, FaultCount = 4, StopCount = 0 });
            WorkshopList.Add(new WorkshopItemModel { Name = "封装车间", TotalCount = 45, WorkCount = 32, WaitCount = 8, FaultCount = 4, StopCount = 0 });
            WorkshopList.Add(new WorkshopItemModel { Name = "焊接车间", TotalCount = 45, WorkCount = 32, WaitCount = 8, FaultCount = 4, StopCount = 0 });
            WorkshopList.Add(new WorkshopItemModel { Name = "贴片车间", TotalCount = 45, WorkCount = 32, WaitCount = 8, FaultCount = 4, StopCount = 0 });
            WorkshopList.Add(new WorkshopItemModel { Name = "贴片车间", TotalCount = 45, WorkCount = 32, WaitCount = 8, FaultCount = 4, StopCount = 0 });
            #endregion

            #region 初始化机台列表
            MachineList = new List<MachineItemModel>();
            for (int i = 0; i < 40; i++)
            {
                int plan = random.Next(100, 1000);
                int complete = random.Next(0, plan);
                MachineList.Add(new MachineItemModel
                {
                    Name = "贴片机-" + (i + 1),
                    CompleteCount = complete,
                    PlanCount = plan,
                    ProgressValue = complete * 1.0 / plan * 100,
                    Status = "作业中",
                    OrderNum = "GD202212345678"
                });
            }

            #endregion


            #region Modbus通信
            Task.Run(() =>
            {
                while (true)
                {
                    using (SerialPort serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One))
                    {
                        serialPort.Open();
                        Modbus.Device.IModbusSerialMaster master = Modbus.Device.ModbusSerialMaster.CreateRtu(serialPort);

                        ushort[] values = master.ReadHoldingRegisters(1, 0, 7);
                        for (int i = 0; i < 7; i++)
                        {
                            Evironment[i].Value = values[i];
                        }

                        serialPort.Close();
                    }
                }
            });
            #endregion
        }
    }
}
